//package com.zy.oauth.shiro;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.serializer.SerializerFeature;
//import com.zy.common.core.R;
//import com.zy.common.core.constant.Constants;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter;
//import org.springframework.http.HttpStatus;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.web.bind.annotation.RequestMethod;
//
//import javax.servlet.ServletRequest;
//import javax.servlet.ServletResponse;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.function.BiConsumer;
//
//@Slf4j
//public class JwtFilter extends BasicHttpAuthenticationFilter {
//
//    private final BiConsumer<ServletResponse, ErrorMessage> writeResponse = (response, message) ->
//            Utils.renderString.accept(
//                    (HttpServletResponse) response,
//                    JSON.toJSONString(R.fail(message), SerializerFeature.WriteMapNullValue)
//            );
//
//    /**
//     * @param request     ServletRequest
//     * @param response    ServletResponse
//     * @param mappedValue mappedValue
//     * @return 是否成功
//     */
//    @Override
//    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//        //input request to request log file
//        log.info(
//                "path:{}, method:{}",
//                httpServletRequest.getServletPath(),
//                httpServletRequest.getMethod()
//        );
//        String token = httpServletRequest.getHeader(Constants.TOKEN);
//        if (token != null) {
//            return executeLogin(request, response);
//        } else {
//            writeResponse.accept(response, ErrorMessage.TOKEN_NOT_EXIST);
//            return false;
//        }
//    }
//
//    /**
//     * execute login
//     */
//    @Override
//    protected boolean executeLogin(ServletRequest request, ServletResponse response) {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//        String token = httpServletRequest.getHeader(Constants.TOKEN);
//        try {
//            JwtDataAuthToken jwtToken = new JwtDataAuthToken(token);
//            // validate user permission
//            getSubject(request, response).login(jwtToken);
//            return true;
//        } catch (AuthenticationException e) {
//            Throwable throwable = e.getCause();
//            if (throwable instanceof TokenExpiredException) {
//                writeResponse.accept(response, ErrorMessage.TOKEN_HAS_EXPIRED);
//            } else {
//                writeResponse.accept(response, ErrorMessage.TOKEN_INVALID);
//            }
//        }
//        return false;
//    }
//
//    /**
//     * support across domains
//     */
//    @Override
//    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
//        HttpServletRequest httpServletRequest = (HttpServletRequest) request;
//        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
//        httpServletResponse.setHeader("Access-Control-Allow-Origin", httpServletRequest.getHeader("Origin"));
//        httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE");
//        httpServletResponse.setHeader("Access-Control-Allow-Headers", httpServletRequest.getHeader("Access-Control-Request-Headers"));
//
//        if (httpServletRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
//            httpServletResponse.setStatus(HttpStatus.OK.value());
//            return false;
//        }
//        return super.preHandle(request, response);
//    }
//
//}
